package com.ruoyi.web.controller.system;

import java.math.BigDecimal;
import java.util.List;

import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.system.domain.RecordMoneys;
import com.ruoyi.system.domain.RetrievalRecord;
import com.ruoyi.system.service.IRecordMoneysService;
import com.ruoyi.system.service.IRetrievalRecordService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.WithdrawDeposit;
import com.ruoyi.system.service.IWithdrawDepositService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 提现Controller
 *
 * @author ruoyi
 * @date 2021-03-08
 */
@RestController
@RequestMapping("/system/deposit")
public class WithdrawDepositController extends BaseController
{
    @Autowired
    private IWithdrawDepositService withdrawDepositService;
    @Autowired
private IRecordMoneysService recordMoneysService;
    @Autowired
    private IRetrievalRecordService retrievalRecordService;
    @Autowired(required = false)
    private TokenService tokenService;
    /**
     * 查询提现列表
     */
    //@PreAuthorize("@ss.hasPermi('system:deposit:list')")
    @GetMapping("/list")
    public TableDataInfo list(WithdrawDeposit withdrawDeposit)
    {
        startPage();
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        Long userId = loginUser.getUser().getUserId();
        System.out.println("idiiiiiiiiiddiiiiiiiiiiiiiiiiiiiiiiiiiiiiii"+userId);
        withdrawDeposit.setCustomerId(userId);
        List<WithdrawDeposit> list = withdrawDepositService.selectWithdrawDepositList(withdrawDeposit);
        return getDataTable(list);
    }

    /**
     * 导出提现列表
     */
    //@PreAuthorize("@ss.hasPermi('system:deposit:export')")
    @Log(title = "提现", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(WithdrawDeposit withdrawDeposit)
    {
        List<WithdrawDeposit> list = withdrawDepositService.selectWithdrawDepositList(withdrawDeposit);
        ExcelUtil<WithdrawDeposit> util = new ExcelUtil<WithdrawDeposit>(WithdrawDeposit.class);
        return util.exportExcel(list, "deposit");
    }

    /**
     * 获取提现详细信息
     */
    //@PreAuthorize("@ss.hasPermi('system:deposit:query')")
    @GetMapping(value = "/{depositId}")
    public AjaxResult getInfo(@PathVariable("depositId") Integer depositId)
    {
        return AjaxResult.success(withdrawDepositService.selectWithdrawDepositById(depositId));
    }

    /**
     * 新增提现
     */
    //@PreAuthorize("@ss.hasPermi('system:deposit:add')")
    @Log(title = "提现", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WithdrawDeposit withdrawDeposit)
    {
        return toAjax(withdrawDepositService.insertWithdrawDeposit(withdrawDeposit));
    }

    /**
     * 修改提现
     */
    //@PreAuthorize("@ss.hasPermi('system:deposit:edit')")
    @Log(title = "提现", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WithdrawDeposit withdrawDeposit)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        Long userId = loginUser.getUser().getUserId();
        RecordMoneys rm = new RecordMoneys();
        WithdrawDeposit wt = new WithdrawDeposit();
        System.out.println("ttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttt"+withdrawDeposit);
        System.out.println(userId);
        rm.setShopsname(withdrawDeposit.getShopsname());     //添加店铺名称
        Double a = new Double(withdrawDeposit.getDepositKetixian());
        rm.setUserId(Integer.parseInt(userId.toString()));   //用户id
        System.out.println(userId);
        rm.setTixiMoney(a);
        rm.setTixiStarte("0");
        rm.setMoney_type("0");
        rm.setNickName(withdrawDeposit.getSupplier_name());
        recordMoneysService.insertRecordMoneys(rm);    //添加一条记录

        System.out.println("我是可提现金额");
        withdrawDeposit.setDepositKetixian(0.00);
        withdrawDeposit.setMoneynumber(0.00);
        return toAjax(withdrawDepositService.updateWithdrawDeposit(withdrawDeposit));
    }

    /**
     * 删除提现
     */
//    @PreAuthorize("@ss.hasPermi('system:deposit:remove')")
    @Log(title = "提现", businessType = BusinessType.DELETE)
	@DeleteMapping("/{depositIds}")
    public AjaxResult remove(@PathVariable Integer[] depositIds)
    {
        return toAjax(withdrawDepositService.deleteWithdrawDepositByIds(depositIds));
    }
}
